/* -----------------------------------------------------------------------------
 * PROJECT    : lwf_handlers
 * FILE       : SumUtil.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Jan 26, 2011  mycodetalks			       Created
 */
package lwf.handler.entities.math;

import java.util.List;

import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.handler.util.ImageHtml;


/**
 * Class description goes here
 */
public class SumUtil {
	
	public static String getOperationImagePath(String operation) {
		if(operation != null && operation.equalsIgnoreCase(IHandlerConstant.SUBTRACTION)) {
			return "image/math/subtraction1.jpeg";
		} else if(operation != null && operation.equalsIgnoreCase(IHandlerConstant.MULTIPLICATION)) {
			return "image/math/multiplication1.jpeg";
		} else if(operation != null && operation.equalsIgnoreCase(IHandlerConstant.DIVISION)) {
			return "image/math/division1.jpeg";
		} 
		return "image/math/addition1.jpeg";
	}
	
	/**
	 * @return Returns the operationsign.
	 */
	public static String getOperationImageHtml(String operation) {
		if(operation != null && operation.equalsIgnoreCase(IHandlerConstant.SUBTRACTION)) {
			return ImageHtml.getImageHtml("image/math/subtraction1.jpeg");
		} else if(operation != null && operation.equalsIgnoreCase(IHandlerConstant.MULTIPLICATION)) {
			return ImageHtml.getImageHtml("image/math/multiplication1.jpeg");
		} else if(operation != null && operation.equalsIgnoreCase(IHandlerConstant.DIVISION)) {
			return ImageHtml.getImageHtml("image/math/division1.jpeg");
		} 
		return ImageHtml.getImageHtml("image/math/addition1.jpeg");
	}
	
	public static String getOperationSign(String operation) {
		
		if(operation != null && operation.equalsIgnoreCase(IHandlerConstant.SUBTRACTION)) {
			return "-";
		} else if(operation != null && operation.equalsIgnoreCase(IHandlerConstant.MULTIPLICATION)) {
			return "x";
		} else if(operation != null && operation.equalsIgnoreCase(IHandlerConstant.DIVISION)) {
			return "/";
		} else if(operation != null && operation.equalsIgnoreCase(IHandlerConstant.ADDITION)) {
			return "+";
		}
		return null;
	}
	
	public static Object getAnswer(String operation, List<Double> data, int decimaldigits) {
		Object answer;
		if(operation.equals(IHandlerConstant.ADDITION)) {
			answer = processAdd(data);
		}
		else if(operation.equals(IHandlerConstant.SUBTRACTION)) {
			answer = processSubtract(data);
		}
		else if(operation.equals(IHandlerConstant.MULTIPLICATION)) {
			answer = processMultiplication(data);
		}
		else {
			//Division
			answer = processDevide(data, decimaldigits);
		}
		return answer;
	}

	private static Object processAdd(List<Double> data) {
		double temp = 0;
		for(int i=0; i<data.size(); i++) {
			temp += ((Double) data.get(i)).doubleValue();
		}
		return new Double(temp);
	}
	
	private static Object processSubtract(List<Double> data) {
		double temp = ((Double) data.get(1)).doubleValue() - ((Double) data.get(0)).doubleValue();
		return new Double(temp);
	}
	
	private static Object processMultiplication(List<Double> data) {
		double temp = ((Double) data.get(1)).doubleValue() * ((Double) data.get(0)).doubleValue();
		return new Double(temp);
	}
	
	private static Object processDevide(List<Double> data, int decimaldigits) {
		double val1 = ((Double) data.get(1)).doubleValue();
		double val2 = ((Double) data.get(0)).doubleValue();

		if(decimaldigits == 0) {
			double[] devideans = new double[2];
			
			//For Quotient
			try {
				devideans[0] = (int) val1/ (int) val2;
			}
			catch(ArithmeticException aex) {
				devideans[0] = 0;
			}
			//For Remainder
			try {
				devideans[1] = (int)val1 % (int)val2;
			}
			catch(ArithmeticException aex) {
				devideans[1] = 0;
			}
			return devideans;
		}
		
		return val1/val2;

	}

}



